//
//  XDSearchView.m
//  XDCommonApp
//
//  Created by XD-XY on 3/7/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "XDSearchView.h"
#import "XDHeader.h"
@implementation XDSearchView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self makeView];
    }
    return self;
}

-(void)makeView
{
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 12, 300, 50/2.0f)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    
    xRect =_bgView.frame;
    
    self.searchIconView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 4, 17, 17)];
    _searchIconView.image = [UIImage imageNamed:@"searchicon"];
    [_bgView addSubview:_searchIconView];
    
    self.mytextField = [[UITextField alloc] initWithFrame:CGRectMake(width_x(_searchIconView)+15, 0, 300-15-(width_x(_searchIconView)), 50/2.0f)];
    _mytextField.backgroundColor = [UIColor clearColor];
    _mytextField.borderStyle = UITextBorderStyleNone;
    _mytextField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    _mytextField.font = [UIFont systemFontOfSize:12.5];
    _mytextField.textColor = TEXTBLACKCOLORE;
    _mytextField.placeholder = @"搜索作品或艺术家";
    _mytextField.clearButtonMode =UITextFieldViewModeAlways;
    _mytextField.delegate =self;
    _mytextField.returnKeyType = UIReturnKeySearch;
    [_mytextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_bgView addSubview:_mytextField];
    
    self.clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _clearBtn.frame = CGRectMake(543/2.0f, 0, 320-543/2.0f, self.frame.size.height);
    [_clearBtn setTitle:@"取消" forState:UIControlStateNormal];
    _clearBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [_clearBtn setAdjustsImageWhenDisabled:NO];
    _clearBtn.backgroundColor  = [UIColor clearColor];
    _clearBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_clearBtn setTitleEdgeInsets:UIEdgeInsetsMake(0,0, 0,18)];
    [_clearBtn setTitleColor:TEXTBLACKCOLORE forState:UIControlStateNormal];
    [_clearBtn addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchDown];
    _clearBtn.alpha =0;
    [self addSubview:_clearBtn];
}

-(void)setBgViewRect:(CGRect)rect
{
    xRect =rect;
    self.bgView.frame = rect;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [_mytextField resignFirstResponder];
    [self changeBgview:NO];
    [_delegate XDSearchViewShouldReturn];
    return YES;
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self changeBgview:YES];
    [_delegate XDSearhViewShouldBeginEditing];
}

-(void)clearClick
{
    [self changeBgview:NO];
    [_mytextField resignFirstResponder];
    [_delegate XDSearchViewCanelbuttonClick];
    
}


-(void)changeBgview:(BOOL)ischange
{
    if (ischange){
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        _bgView.frame = CGRectMake(xRect.origin.x,xRect.origin.y, 246, 50/2.0f);
        _mytextField.frame =CGRectMake(width_x(_searchIconView)+15, 0, 246-15-(width_x(_searchIconView)), 50/2.0f);
        _clearBtn.alpha =1;
        [UIView commitAnimations];
    }else{
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];
        _bgView.frame = CGRectMake(xRect.origin.x, xRect.origin.y, 300, 50/2.0f);
        _mytextField.frame = CGRectMake(width_x(_searchIconView)+15, 0, 300-15-(width_x(_searchIconView)), 50/2.0f);
        [UIView commitAnimations];
        _clearBtn.alpha=0;
    }
}

-(void)textFieldDidChange:(UITextField *)textField
{
    [_delegate XDSearchTextFieldDidChange];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
